import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http';
import { Observable, of } from 'rxjs';

import { environment } from '../environments/environment';

import { Comment } from '../models/Comment';
import { CommentReply } from '../models/CommentReply';
import { CommentZan } from '../models/CommentZan';
@Injectable()
export class ForumDetailService {
 // 评论
  private commentUrl = environment.apiUrl + 'api/comment';
  // 回复
  private  replyUrl = environment.apiUrl + 'api/reply';
  // 点赞
  private  zanUrl = environment.apiUrl + 'api/zan';

  constructor(
    private http: HttpClient
  ) {}
  // 评论
  getComments(current: number = 0, pageSize: number = environment.pageSize, newsId: string)
    : Observable<{res: Comment[], success: boolean, total: number}> {
    let params: HttpParams = new HttpParams();
    params = params.append('page', String(current + 1));
    params = params.append('pageSize', String(pageSize));
    params = params.append('newsId', newsId);
    return this.http.get<{res: Comment[], success: boolean, total: number}>(`${this.commentUrl}/getlist`, { params: params})
  }
  getCount(newsId: number): Observable<{res: number, success: boolean}> {
    return this.http.get<{res: number, success: boolean}>(`${this.commentUrl}/getCount?newsId=${newsId}`);
  }
  deleteComment(id): Observable<{success: boolean}> {
    return this.http.delete<{success: boolean}>(`${this.commentUrl}/deleteComment?id=${id}`);
  }
  insertComment(comment: Comment): Observable<{res: Comment, success: boolean}> {
    return this.http.post<{res: Comment, success: boolean}>(`${this.commentUrl}/insertComment`, comment);
  }
  // 回复区
  // getReply(current: number = 0, pageSize: number = environment.pageSize): Observable<{res: CommentReply[], success: boolean, total: number}> {
  //   let params: HttpParams = new HttpParams();
  //   params = params.append('page', String(current + 1));
  //   params = params.append('pageSize', String(pageSize));
  //   return this.http.get<{res: CommentReply[], success: boolean, total: number}>(`${this.replyUrl}/getlist`, { params: params})
  // }
  getReply(id): Observable<{res: CommentReply[], success: boolean}> {
    return this.http.get<{res: CommentReply[], success: boolean}>(`${this.replyUrl}/getlist?commentId=${id}`)
  }
  insertReply(reply: CommentReply): Observable<{res: CommentReply, success: boolean}>{
    return this.http.post<{res: CommentReply, success: boolean}>(`${this.replyUrl}/insertReply`, reply);
  }
  deleteReply(id: string): Observable<{success: boolean}>{
    return this.http.delete<{success: boolean}>(`${this.replyUrl}`);
  }
  // 点赞区
  getNewsCounts(typeId: number): Observable<{res: number, success: boolean}> {
    return this.http.get<{res: number, success: boolean}>(`${this.zanUrl}/getNewsCount?typeId=${typeId}`);
  }
  getNewsZan(typeId: string): Observable<{res: number, success: boolean}> {
    return this.http.get<{res: number, success: boolean}>(`${this.zanUrl}/getNewsCount`);
  }
  insertZan(commentZan: CommentZan): Observable<{res: CommentZan, success: boolean}> {
    return this.http.post<{res: CommentZan, success: boolean}>(`${this.zanUrl}/insertZan`, commentZan);
  }
  updateZan(commentZan: CommentZan): Observable<{res: CommentZan, success: boolean}> {
    return this.http.post<{res: CommentZan, success: boolean}>(`${this.zanUrl}/updateZan`, commentZan);
  }
}
